layui.use(['grid'], function () {
    var pager = layui.grid
        , tableId = "drug-table"
        , tableFilter = "drug-table-filter"
        , searchFilter = "drug-search"
        , cols = [
        {width: 60, field: "_serial", title: "序号"},
        {field: "name", title: "产品名称"},
        {field: "company", title: "公司名称"},
        {field: "desc_id", title: "风险描述"},
        {field: "analysis_id", title: "风险分析"},
        {field: "reported_at", title: "上报时间"},
        {field: "unit", title: "上报单位"},
        {field: "address", title: "所属地区"},
        {field: "phone", title: "联系电话"},
        {fixed: 'right', width: 150, align: 'center', toolbar: '#tool'}
    ]
        , table = {
        "id": tableId,
        "elem": "#" + tableId,
        "limit": 8,
        "limits": [10, 20, 50, 100],
        "cols": [cols],
        "method": "post",
        "request": {"pageName": "page", "limitName": "per-page"},
        "url": "/report-ready-list",
        "page": {"hash": "fenye"},
        "autoSort": false,
        "defaultToolbar": []
    }
        , eventOptions = {
        "watch": {"url": "/report-watch"},
        "unwatch": {"url": "/report-unwatch"},
    }
        , events = {
        "watch": function (obj, config, that) {
            var helper = layui.helper, $ax = layui.ax, layer = layui.layer
            layer.prompt({
                formType: 2, //输入框类型，支持0（文本）默认1（密码）2（多行文本）
                value: '无', //初始时的值，默认空字符
                maxlength: 500, //可输入文本的最大长度，默认500
                title: '请输入风险评估'
            }, function (value, index, elem) {
                var url = config.url + '?' + helper.urlEncode({id: obj.data['id']});
                var ajax = new $ax(url, function (res) {
                    that.reloadTable(null, null, true);
                    layer.close(index)
                });
                ajax.setMethod('post')
                ajax.setData({comment: value});
                ajax.send();
            });
        },
        "unwatch": function (obj, config, that) {
            var $ax = layui.ax, helper = layui.helper
            layer.alert("确定忽略该报告？", function (index) {
                var ajax = new $ax(config.url + '?' + helper.urlEncode({id: obj.data['id']}));
                ajax.setMethod('post');
                ajax.setResult(function (result) {
                    that.reloadTable(null, null, true);
                    layer.close(index);
                    layer.msg(result.msg)
                });
                ajax.send();
            })
        }
    }

    pager.render({
        "searchFilter": searchFilter,
        "tableId": tableId,
        "tableFilter": tableFilter,
        "sort": "server",
        "countCache": true,
        "table": table,
        "editor": {},
        "eventOptions": eventOptions
    }, events)
})
